import { marked } from "marked";
const MincodeLinesOfQuestion = 5;
const MincodeLinesOfAnswer = 10;
export function appendBtnToTarget(markedStr, data = {}) {
  const {
    options = {},
    isAnswering = false,
    isQuestion = false,
    needCodeOperateBar = true,
    isExpand = true,
  } = data;
  const container = document.createElement("div");
  container.innerHTML = markedStr;
  const codeEleList = container.querySelectorAll("pre > code");
  codeEleList.forEach((codeEle) => {
    codeEle.classList.add(
      "input-background",
      `ide-language-${options.language || ""}`
    );
    const preEle = codeEle.parentNode;
    preEle.classList.add(
      "pre-code-element",
      isExpand ? "expand-status" : "collapse-status"
    );
    if (needCodeOperateBar) {
      const codeOperateBar = getCodeOperatoBar(options, isAnswering);
      preEle.parentNode.insertBefore(codeOperatoBar, preEle);
    }
    const minCodeLine =
      data.minCodeLine ||
      (isQuestion ? MincodeLinesOfQuestion : MincodeLinesOfAnswer);
    const expandOrCollapseEle = getExpandCollapseEle(
      preEle,
      minCodeLine,
      isAnswering,
      isExpand
    );
    if (expandOrCollapseEle) {
      preEle.after(expandOrCollapseEle);
    }
  });
  return container.innerHTML;
}
